﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Microsoft.OpportunityPortal.RiskEventRcvr
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class RiskEventRcvr : SPItemEventReceiver
    {
        /// <summary>
        /// An item is added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            ProcessListEvents(properties);
        }

        /// <summary>
        /// An item is updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            ProcessListEvents(properties);
        }

        /// <summary>
        /// An item is deleted.
        /// </summary>
        public override void ItemDeleted(SPItemEventProperties properties)
        {
            base.ItemDeleted(properties);

            ProcessListEvents(properties);
        }

        private void ProcessListEvents(SPItemEventProperties properties)
        {
            // Check the list for which event was fired
            string listTitle = properties.ListTitle;
            bool result = false;

            if (properties.EventType == SPEventReceiverType.ItemUpdated ||
                properties.EventType == SPEventReceiverType.ItemDeleted ||
                properties.EventType == SPEventReceiverType.ItemAdded)
            {
                EventFiringEnabled = false;
                result = Risks.ProcessCaseStudyEvent(properties);
                EventFiringEnabled = true;
            }
        }

    }
}
